<?php
/**
 * Author:xsc
 * Time:2023/10/12 17:32
 */

namespace App\Service\Common;

use App\Service\BaseService;
use App\Repository\OpenPlatformRepo;
use App\Exceptions\BusinessException;
use App\Common\ResponseCode;
use App\Extensions\Common\RedisService;
use App\Common\Constant;
use App\Repository\OpenPlatformRouteRepo;

class PlatformService extends BaseService
{

    /**
     * 获取平台信息,带缓存
     * nft平台会更新public_key,type,如果用到这两个字段,请勿使用此方法
     * @param string $platform_no
     * @return array|mixed
     * @throws \RedisException
     * Author:xsc
     * Time:2023/10/13 10:01
     */
    public function getInfoWithCache(string $platform_no)
    {
        $redis = RedisService::getInstance();
        $key = Constant::PLATFORM_INFO . $platform_no;
        $cache = $redis->get($key);
        if ($cache) {
            return json_decode($cache, true);
        }

        // 平台信息
        $platform_info = OpenPlatformRepo::getInstance()->getInfoByPlatformNo($platform_no);
        if ($platform_info) {
            $redis->set($key, json_encode($platform_info, 256), 3600);
        }
        return $platform_info;
    }

    /**
     * 校验平台的路由权限
     * @param int $platform_id
     * @param string $route_name
     * @return bool
     * Author:xsc
     * Time:2023/10/13 10:21
     */
    public function checkPlatformRoute(int $platform_id, string $route_name)
    {
        $platform_route = OpenPlatformRouteRepo::getInstance()->checkRoute($platform_id, $route_name);
        if (!$platform_route) {
            return false;
        }
        return true;
    }

}
